﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace VPLink2013.Batch
{
    public static class XmlManager
    {
        public static XElement CreateNode(string nodeName, Dictionary<string, string> properties)
        {
            XElement newNode = new XElement(nodeName);
            foreach (string key in properties.Keys)
            {
                newNode.SetAttributeValue(key, properties[key]);
            }
            return newNode;
        }

        public static bool isAttributeValue(this XElement input, string attributeName, string attributeValue)
        {
            bool found = false;
            if (input.Attribute(attributeName) != null)
            {
                if (input.Attribute(attributeName).Value.ToLower() == attributeValue.ToLower())
                {
                    found = true;
                }
            }
            return found;
        }
        private static bool AttributesMatch(this XElement elem, Dictionary<string, string> attributes)
        {
            bool attributesMatch = true;
            int i = 0;
            while (i < attributes.Keys.Count() && attributesMatch == true)
            {
                string key = attributes.Keys.ElementAt(i);
                if (elem.Attribute(key) != null)
                {
                    if (string.Compare(elem.Attribute(key).Value, attributes[key], StringComparison.CurrentCultureIgnoreCase) != 0)
                    {
                        attributesMatch = false;
                    }
                }
                i++;
            }
            return attributesMatch;
        }

        public static List<XElement> GetAllElementsByAttributes(this XElement parentService, string nodeName, Dictionary<string, string> attributes)
        {
            return parentService.Elements(nodeName).Where(x => x.AttributesMatch(attributes)).ToList<XElement>();
        }

        public static XElement GetUniqueElementByAttributes(this XElement parentService, string nodeName, Dictionary<string, string> attributes)
        {
            var elem = parentService.Elements(nodeName).Where(x => x.AttributesMatch(attributes));
            if (elem != null)
            {
                return elem.FirstOrDefault();
            }
            else
            {
                return null;
            }
        }

        //private static int CompareElementsByAttributeValueAsString( XElement e1, XElement e2, string attributeName )
        //{
        //    if( string.Compare( e1.Attribute( attributeName ).Value, e2.Attribute( attributeName ).Value ) == 0 )
        //    {
        //        return string.Compare( e1.Attribute( "FirstName" ).Value, e2.Attribute( "FirstName" ).Value );
        //    }
        //    else
        //    {
        //        return string.Compare( e1.Attribute( "LastName" ).Value, e2.Attribute( "LastName" ).Value );
        //    }
        //}

        #region Label tags
        public static void SetAttributeToParents(this XElement childElement, string topNode, string attributName, string attributValue)
        {
            bool topParentReach = false;
            XElement iter = childElement;
            while (!topParentReach)
            {
                if (topNode.Equals(iter.Parent.Name.ToString()))
                {
                    topParentReach = true;
                }
                else
                {
                    iter = iter.Parent;
                    if (!iter.isAttributeValue(attributName, attributValue))
                    {
                        iter.SetAttributeValue(attributName, attributValue);
                    }
                }
            }
        }
        #endregion

        //public static XElement SortElementsByName( this XElement parentService,  List<XElement> childsToSort)
        //{
        //    parentService.Elements().ToList<XElement>().Sort( ( x, y ) => CompareByName( x, y ) );
        //    return parentService.Elements().Where( x => x.Attribute( "IsManager" ).Value == "false" ).OrderBy( x => x.Attribute( "LastName" ) );
        //}

    }
}
